package com.st.bottomnavigation.ui;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.st.bottomnavigation.R;
import com.st.bottomnavigation.service.MyService;
import com.st.bottomnavigation.service.TestService;
import com.st.bottomnavigation.ui.activity.BaseActivity;
import com.st.bottomnavigation.utils.RetrofitUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class TestActivity extends BaseActivity {

    private static String TAG = "TestActivity";
    private TestService testService;
    private Retrofit retrofit;
    private TextView tt;
    private MyService apiService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    protected int getContentViewId() {
        return R.layout.activity_test;
    }

    @Override
    protected String getHeaderTitle() {
        return "测试";
    }
    @Override
    public void initView() {
        super.initView();
        tt = findViewById(R.id.tt);
        testService = RetrofitUtil.build(TestService.class);

        //
        //Retrofit retrofit1 = RetrofitUtil.build();
        //apiService = retrofit1.create(MyService.class);

    }

    @Override
    public void listener() {

    }

    // get同步请求
    public void getSync(View v)  {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Response<ResponseBody> execute = testService.get("小满", "123").execute();
                    String result = testService.get("小满", "123").execute().body().string();
                    Log.i(TAG, "getSync: "+result);
                    tt.setText("get同步请求:"+result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    // get异步请求
    public void getAsync(View view) {
        testService.get("小满","123").enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (response.isSuccessful()){
                        String string = response.body().string();
                        Log.i(TAG, "onResponse: "+string);
                        tt.setText("get异步请求:"+string);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println(throwable.getMessage());
            }
        });
    }

    public void getAsyncMap(View view) {
        Map<String,String> map = new HashMap<>();
        map.put("username","小满");
        map.put("password","123");
        testService.getQueryMap(map).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (response.isSuccessful()){
                        String string = response.body().string();
                        Log.i(TAG, "onResponse: "+string);
                        tt.setText("get异步请求:"+string);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {

            }
        });
    }

    public void getAsyncPath(View view) {
        testService.getInPath("小满","android").enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (response.isSuccessful()){
                        String string = response.body().string();
                        Log.i(TAG, "onResponse: "+string);
                        tt.setText("get异步请求:"+string);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println(throwable.getMessage());
            }
        });
    }

    // post同步请求
    public void postSync(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String result = testService.postFrom("小满", "123").execute().body().string();
                    Log.i(TAG, "onResponse: "+result);
                    tt.setText("post同步请求:"+result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    // post异步请求
    public void postAsync(View v) {
        Call<ResponseBody> call = testService.postFrom("小满","123");
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (response.isSuccessful()){
                        String string = response.body().string();
                        Log.i(TAG, "onResponse: "+string);
                        tt.setText("post异步请求:"+string);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println(throwable.getMessage());
            }
        });
    }

    public void postFieldMap(View view) {
        Map<String,String> map = new HashMap<>();
        map.put("username","小满");
        map.put("password","123");
        testService.postFiledMap(map).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (response.isSuccessful()){
                        String string = response.body().string();
                        Log.i(TAG, "onResponse: "+string);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {

            }
        });
    }

    // post body请求
    public void postBody(View view) {
        FormBody formBody = new FormBody.Builder()
                .add("username", "小满postBody")
                .add("password", "123")
                .build();
        testService.postBody(formBody).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (response.isSuccessful()){
                        String string = response.body().string();
                        Log.i(TAG, "onResponse: "+string);
                        tt.setText("http注解请求:"+string);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                System.out.println(throwable.getMessage());
            }
        });
    }

    public void httpAnnotation(View view) {
        testService.http("小满","123").enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (response.isSuccessful()){
                        String string = response.body().string();
                        Log.i(TAG, "onResponse: "+string);
                        tt.setText("http注解请求:"+string);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {
                 System.out.println(throwable.getMessage());
            }
        });
    }


    public void getUrl(View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                testService.getUrl("https://www.httpbin.org/get").enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            if (response.isSuccessful()){
                                String string = response.body().string();
                                Log.i(TAG, "onResponse: "+string);
                                tt.setText("自定义请求:"+string);
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable throwable) {

                    }
                });
            }
        }).start();

    }

    public void postJson(View view) {
        JSONObject json = new JSONObject();
        json.put("username","小满");
        json.put("password","123");
        json.put("type","json格式-post");
        RequestBody newBody = RequestBody.create(MediaType.parse("application/json"), json.toJSONString());
        testService.postJson(newBody).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    if (response.isSuccessful()){
                        String string = response.body().string();
                        Log.i(TAG, "onResponse: "+string);
                        tt.setText("json格式请求:"+string);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable throwable) {

            }
        });
    }
}